package vn.egame.elockscreen.object;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

public class AnimationTexture extends BaseActor{
	private Texture[] texture;

	private int duration;
	private int frameDuration;
	private int currentFrame = 0;
	private long lastTimeDraw;
	private int widthFrame, heightFrame;
	private boolean finish;
	private boolean repeat;
	private boolean draw;

	public AnimationTexture(Texture[] texture, int frameDuration) {
		super();
		this.texture = texture;

		this.frameDuration = frameDuration;
		this.duration = this.frameDuration * this.texture.length;
		this.currentFrame = 0;
		widthFrame = texture[0].getWidth();
		heightFrame = texture[0].getHeight();
		lastTimeDraw = System.currentTimeMillis();
		repeat = true;
		draw = true;

	}
	
	public AnimationTexture(Texture[] texture, int frameDuration,boolean repeat) {
		super();
		this.texture = texture;

		this.frameDuration = frameDuration;
		this.duration = this.frameDuration * this.texture.length;
		this.currentFrame = 0;
		widthFrame = texture[0].getWidth();
		heightFrame = texture[0].getHeight();
		lastTimeDraw = System.currentTimeMillis();
		this.repeat = repeat;
		draw = true;

	}
	
	public void setRepeat(){
		draw = true;
		finish = false;
	}

	public void update() {

		if (System.currentTimeMillis() - lastTimeDraw > frameDuration) {

			lastTimeDraw = System.currentTimeMillis();

			currentFrame = ++currentFrame;

			Log.d("Kai", currentFrame + "");

		}
	}

	public void onDraw(Canvas canvas, int x, int y) {

		Rect src = new Rect(0, 0, widthFrame, heightFrame);
		Rect dst = new Rect(x, y, x + widthFrame, y + heightFrame);
		if (currentFrame >= this.texture.length) {
			currentFrame = 0;
			finish = true;
			if(finish && !repeat){
				draw = false;
			}
		}
		if (draw) {
			canvas.drawBitmap(texture[currentFrame].getTexture(), src, dst,
					null);
		}

	}

	public void onDraw(Canvas canvas, int x, int y, int position) {

		Rect src = new Rect(0, 0, widthFrame, heightFrame);
		Rect dst = new Rect(x, y, x + widthFrame, y + heightFrame);

		canvas.drawBitmap(texture[position].getTexture(), src, dst, null);

	}

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getFrameDuration() {
		return frameDuration;
	}

	public void setFrameDuration(int frameDuration) {
		this.frameDuration = frameDuration;
	}

	public int getWidthFrame() {
		return widthFrame;
	}

	public void setWidthFrame(int widthFrame) {
		this.widthFrame = widthFrame;
	}

	public int getHeightFrame() {
		return heightFrame;
	}

	public void setHeightFrame(int heightFrame) {
		this.heightFrame = heightFrame;
	}

	public boolean isFinish() {
		return finish;
	}

	public void setFinish(boolean finish) {
		this.finish = finish;
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}

}
